//
//  NotiListTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/3/11.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "NotiListTableViewCell.h"

@interface NotiListTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * subLabel;
@property (nonatomic, weak)UIButton * agreeButton;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * stausLabel;
@property (nonatomic, weak)UIImageView * isReadImageView;

@end

@implementation NotiListTableViewCell


- (void)setModel:(MessageNotiModel *)model{
    
    _model = model;
    
    NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.group_img];
    NSURL * url = [NSURL URLWithString:imgStr];
    [self.iconImageView sd_setImageWithURL:url];
    
    NSArray * imgArr = [model.inputtime componentsSeparatedByString:@" "];
    NSString * time = [NSString stringWithFormat:@"%@",imgArr.firstObject];
    
    int is_see = [model.is_see intValue];
    
    if (is_see == 1) {
        
        self.isReadImageView.hidden = YES;
    }else{
        
        self.isReadImageView.hidden = NO;
    }
    
    int type = [model.inform_type intValue];
    
    if (type == 1) {
        
        self.nameLabel.text = model.user_name;
        self.subLabel.text = [NSString stringWithFormat:@"%@申请加入%@",model.user_name,model.group_name];
        int status = [model.audit_status intValue];
        //入团审核状态  (0为待处理  1为同意  2为拒绝)
        if (status == 0) {
            
            self.agreeButton.hidden = NO;
            self.timeLabel.hidden = YES;
            self.stausLabel.hidden = YES;
        }else if (status == 1) {
           
            self.agreeButton.hidden = YES;
            self.timeLabel.hidden = NO;
            self.stausLabel.hidden = NO;
            
            self.timeLabel.text = time;
            self.stausLabel.text = @"已同意";
        }else if (status == 2){
            
            self.agreeButton.hidden = YES;
            self.timeLabel.hidden = NO;
            self.stausLabel.hidden = NO;
            
            self.timeLabel.text = time;
            self.stausLabel.text = @"已拒绝";
        }
        
    }else if (type == 2){
        
        self.nameLabel.text = model.group_name;
        self.subLabel.text = [NSString stringWithFormat:@"社长%@已解散%@",model.user_name,model.group_name];
        self.agreeButton.hidden = YES;
        self.timeLabel.hidden = NO;
        self.stausLabel.hidden = YES;
        self.timeLabel.text = time;
    }else if (type == 3){
        
        self.nameLabel.text = model.group_name;
        self.subLabel.text = [NSString stringWithFormat:@"你被%@移除%@",model.user_name,model.group_name];
        self.agreeButton.hidden = YES;
        self.timeLabel.hidden = NO;
        self.stausLabel.hidden = YES;
        self.timeLabel.text = time;
    }else if (type == 4){
        
        self.nameLabel.text = model.group_name;
        self.subLabel.text = [NSString stringWithFormat:@"%@邀请你加入%@",model.user_name,model.group_name];
        self.agreeButton.hidden = YES;
        self.timeLabel.hidden = NO;
        self.stausLabel.hidden = YES;
        self.timeLabel.text = time;
    }else if (type == 5){
        
        self.nameLabel.text = model.group_name;
        self.subLabel.text = [NSString stringWithFormat:@"%@拒绝你加入%@",model.user_name,model.group_name];
        self.agreeButton.hidden = YES;
        self.timeLabel.hidden = NO;
        self.stausLabel.hidden = YES;
        self.timeLabel.text = time;
    }else{
        
        self.subLabel.text = @"没什么新鲜事~~";
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF0F0F0);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor  =ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
//    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.layer.cornerRadius = 20 * SCREENH6;
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor =ZRBlackColor;
    nameLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
//    nameLabel.text = @"";
    
    self.nameLabel = nameLabel;
    
    [self.backView addSubview:self.nameLabel];
    
    
    UILabel * subLabel = [UILabel new];
    
    subLabel.textColor =[UIColor lightGrayColor];
    subLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    //    nameLabel.text = @"";
    
    self.subLabel = subLabel;
    
    [self.backView addSubview:self.subLabel];
    
    
    UIButton * agreeButton = [UIButton new];
    
    [agreeButton setTitle:@"同意" forState:UIControlStateNormal];
    [agreeButton setTitleColor:ColorWithRGB(0xFFE035) forState:UIControlStateNormal];
    [agreeButton addTarget:self action:@selector(clickAgreeButton) forControlEvents:UIControlEventTouchUpInside];
    agreeButton.layer.borderColor = [ColorWithRGB(0xFFE035) CGColor];
    agreeButton.layer.borderWidth = 1.0f;
    agreeButton.layer.cornerRadius = 5 * SCREENH6;
    agreeButton.layer.masksToBounds = YES;
    agreeButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.agreeButton = agreeButton;
    
    [self.backView addSubview:self.agreeButton];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor =[UIColor lightGrayColor];
    timeLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    timeLabel.textAlignment = NSTextAlignmentRight;
    //    nameLabel.text = @"";
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UILabel * stausLabel = [UILabel new];
    
    stausLabel.textColor =[UIColor lightGrayColor];
    stausLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    stausLabel.textAlignment = NSTextAlignmentRight;
    //    nameLabel.text = @"";
    
    self.stausLabel = stausLabel;
    
    [self.backView addSubview:self.stausLabel];
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:@"Ellipse3"];
    
    self.isReadImageView = imageView;
    
    [self.backView addSubview:self.isReadImageView];
}


-(void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(60 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.height.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.width.mas_equalTo(230 * SCREENH6);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(7 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.nameLabel).offset(3 * SCREENH6);
    }];
    
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
        make.width.mas_equalTo(40 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.stausLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.timeLabel);
        make.centerY.mas_equalTo(self.subLabel);
    }];
    
    [self.isReadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView.mas_trailing).offset(-5 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(5 * SCREENH6);
        //        make.centerY.mas_equalTo(self.whiteView);
        make.height.width.mas_equalTo(8 * SCREENH6);
    }];
}


- (void)clickAgreeButton{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickAgreeButtonWithID:)])
    {
        NSString * ID = [NSString stringWithFormat:@"%@",self.model.id];
        [self.delegate clickAgreeButtonWithID:ID];
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
